﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Smarly.Core;
using Smarly.Core.Database;

namespace Smarly.Ebook.Model
{
	public class ProfileEbook
	{
		public string EbookUrl { get; set; }

		private Guid BookId { get; set; }
		public int Cost { get; set; }
		public string PurchaseText { get; set; }

		public Book CurrentBook { get; set; }
		public List<Book> Books { get; set; }

		public class Book
		{
			public Guid Id { get; set; }
			public string Name { get; set; }
			public string HashTag { get; set; }
			public string Author { get; set; }
			public string Cover { get; set; }
			public string Description { get; set; }

		}

		public ProfileEbook Get(SmarlyIdentity identity, string hashTag)
		{
			return SqlRepository.ExecuteProcedureReader("Ebook_Item_Get", new List<SpParam>
				                                                              {
					                                                              new SpParam
						                                                              {
							                                                              Name = "@userId",
							                                                              Value = identity.UserId,
							                                                              Type = DbType.Guid
						                                                              },
					                                                              new SpParam
						                                                              {
							                                                              Name = "@hashTag",
							                                                              Value = hashTag,
							                                                              Type = DbType.String
						                                                              },
				                                                              }, null,
			                                            (reader) =>
				                                            {
					                                            int batchNum = 0;
					                                            do
					                                            {
						                                            switch (batchNum)
						                                            {
							                                            case 0:
								                                            while (reader.Read())
								                                            {
									                                            EbookUrl = reader.GetStringOrNull("EbookUrl");
									                                            PurchaseText = reader.GetStringOrNull("PurchaseText");
									                                            BookId = reader.GetGuidOrDefault("BookId");
									                                            Cost = reader.GetInt32OrDefault("Cost");
								                                            }
								                                            break;
							                                            case 1:
								                                            Books = new List<Book>();
								                                            while (reader.Read())
								                                            {
									                                            var item = new Book
										                                                       {
											                                                       Id = reader.GetGuid("BookId"),
											                                                       Author = reader.GetString("Author"),
											                                                       Cover = reader.GetString("CoverUrl"),
											                                                       HashTag = reader.GetString("HashTag"),
											                                                       Name = reader.GetString("Text"),
										                                                       };

									                                            if (item.Id == BookId)
										                                            CurrentBook = item;

																															Books.Add(item);

								                                            }
								                                            break;
							                                            case 2:
								                                            StringBuilder sb = new StringBuilder();
								                                            while (reader.Read())
								                                            {
									                                            sb.Append(reader.GetString("Text"));
								                                            }

								                                            CurrentBook.Description = sb.ToString();

								                                            break;
							                                            default:
								                                            throw new ApplicationException("Additional batch select");

						                                            }
						                                            batchNum++;
					                                            } while (reader.NextResult());
					                                            return this;
				                                            });

		}
	}
}